﻿using AutoMapper;
using BackData;
using Domain.BOM;
using Domain.DTOs.BOMs;
using Infratructure;
using MediatR;
using Team.API.Applocation.Command.BOM;

namespace Team.API.Applocation.Hanldler.BOM
{
    public class GetBOMDetailsHandler:IRequestHandler<GetBOMDetailsCommand,APIResult<BOMDTO>>
    {
        private readonly IRepository<Bomtable> bomway;
        private readonly IMapper mapper;

        public GetBOMDetailsHandler(IRepository<Bomtable> bomway,IMapper mapper)
        {
            this.bomway = bomway;
            this.mapper = mapper;
        }
        /// <summary>
        /// 获取BOM成品详情
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public  Task<APIResult<BOMDTO>> Handle(GetBOMDetailsCommand request, CancellationToken cancellationToken)
        {
            var res=new APIResult<BOMDTO>();
            var value= bomway.GetAll().Where(x=>x.Id==request.Id&&x.IsDel==false).First();
            if (value != null)
            {
                var result=mapper.Map<BOMDTO>(value);
                res.Code=StateCode.OK;                    
                res.Message="成功";
                res.Result=result;
              
            }
            else
            {
                 res.Code=StateCode.Fail;
                res.Message="BOM详情获取失败";
              
            }
            return    Task.FromResult(res);
        }
    }
}
